package com.ruoyi.yzchu.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.yzchu.domain.YzcProduct;
import com.ruoyi.yzchu.mapper.YzcShopMapper;
import com.ruoyi.yzchu.domain.YzcShop;
import com.ruoyi.yzchu.service.IYzcShopService;

/**
 * 店铺Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-06
 */
@Service
public class YzcShopServiceImpl implements IYzcShopService 
{
    @Autowired
    private YzcShopMapper yzcShopMapper;

    /**
     * 查询店铺
     * 
     * @param shopId 店铺主键
     * @return 店铺
     */
    @Override
    public YzcShop selectYzcShopByShopId(Long shopId)
    {
        return yzcShopMapper.selectYzcShopByShopId(shopId);
    }

    /**
     * 查询店铺列表
     * 
     * @param yzcShop 店铺
     * @return 店铺
     */
    @Override
    public List<YzcShop> selectYzcShopList(YzcShop yzcShop)
    {
        return yzcShopMapper.selectYzcShopList(yzcShop);
    }

    /**
     * 新增店铺
     * 
     * @param yzcShop 店铺
     * @return 结果
     */
    @Transactional
    @Override
    public int insertYzcShop(YzcShop yzcShop)
    {
        yzcShop.setCreateTime(DateUtils.getNowDate());
        int rows = yzcShopMapper.insertYzcShop(yzcShop);
        insertYzcProduct(yzcShop);
        return rows;
    }

    /**
     * 修改店铺
     * 
     * @param yzcShop 店铺
     * @return 结果
     */
    @Transactional
    @Override
    public int updateYzcShop(YzcShop yzcShop)
    {
        yzcShopMapper.deleteYzcProductByShopId(yzcShop.getShopId());
        insertYzcProduct(yzcShop);
        return yzcShopMapper.updateYzcShop(yzcShop);
    }

    /**
     * 批量删除店铺
     * 
     * @param shopIds 需要删除的店铺主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYzcShopByShopIds(Long[] shopIds)
    {
        yzcShopMapper.deleteYzcProductByShopIds(shopIds);
        return yzcShopMapper.deleteYzcShopByShopIds(shopIds);
    }

    /**
     * 删除店铺信息
     * 
     * @param shopId 店铺主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYzcShopByShopId(Long shopId)
    {
        yzcShopMapper.deleteYzcProductByShopId(shopId);
        return yzcShopMapper.deleteYzcShopByShopId(shopId);
    }

    /**
     * 新增商品管理信息
     * 
     * @param yzcShop 店铺对象
     */
    public void insertYzcProduct(YzcShop yzcShop)
    {
        List<YzcProduct> yzcProductList = yzcShop.getYzcProductList();
        Long shopId = yzcShop.getShopId();
        if (StringUtils.isNotNull(yzcProductList))
        {
            List<YzcProduct> list = new ArrayList<YzcProduct>();
            for (YzcProduct yzcProduct : yzcProductList)
            {
                yzcProduct.setShopId(shopId);
                list.add(yzcProduct);
            }
            if (list.size() > 0)
            {
                yzcShopMapper.batchYzcProduct(list);
            }
        }
    }
}
